package com.boarsoft.boar.soagov.batch.health;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

import com.boarsoft.boar.app.AppHandler;
import com.boarsoft.boar.service.AppInstService;
import com.boarsoft.flow.core.bean.ProcessHandler;

/**
 * 检查某个应用实例的状态
 * 
 * @author Mac_J
 *
 */
public class AppInstHealthChkPH implements ProcessHandler {
	private static final Logger log = LoggerFactory.getLogger(AppInstHealthChkPH.class);

	@Autowired
	protected ApplicationContext appCtx;
	@Autowired
	protected AppInstService appInstSvc;

	@SuppressWarnings("unchecked")
	@Override
	public Object process(String entry, Object data, Throwable throwable) throws Throwable {
		Map<String, Object> map = (Map<String, Object>) data;
		String appInstId = (String) map.get("appInstId");
		String handler = (String) map.get("handler");
		String addr = (String) map.get("addr");
		AppHandler ah = appCtx.getBean(AppHandler.class, handler);
		short status = ah.test(addr);
		log.info("Status of app inst {} is {}", appInstId, status);
		appInstSvc.status(appInstId, status);
		return data;
	}
}